﻿namespace Json2Model
{
    public class JModel
    {
        /// <summary>
        /// 类名
        /// </summary>
        public string Name { get; set; } = string.Empty;
        /// <summary>
        /// 属性
        /// </summary>
        public IList<JAttribute> Attributes { get; set; } = new List<JAttribute>();

        public static bool operator ==(JModel l, JModel r)
        {
            if (!(l is JModel) || !(r is JModel)) return false;
            bool res = l.Name == r.Name && l.Attributes.Count == r.Attributes.Count;
            if(!res) return false;
            foreach (var item in l.Attributes)
            {
                 if(!r.Attributes.Contains(item))
                    return false;
            }
            return true;         
        }
        public static bool operator !=(JModel l, JModel r)
        {
            return !(l == r);
        }

        public override bool Equals(object? obj)
        {
            if (!(obj is JModel))
                return false;
            else
            {
                JModel? model = obj as JModel;
                bool res = Name == model!.Name && Attributes.Count == model.Attributes.Count;
                if (!res) return false;
                foreach (var item in model.Attributes)
                {
                    if (!Attributes.Contains(item))
                        return false;
                }
                return true;
            }
        }

        public override int GetHashCode()
        {
            int res = 0;
            foreach (var item in Attributes)
            {
                res+=item.GetHashCode();
            }
            return res + Name.GetHashCode();
        }
    }
}
